package com.example.eeuse.controller;


import cn.hutool.core.util.IdUtil;
import com.example.eeuse.mapper.DocumentMapper;
import com.example.eeuse.model.Document;
import com.example.eeuse.model.DocumentDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * 测试使用Easy-ES
 *
 * Copyright © 2021 xpc1024 All Rights Reserved
 **/

@Slf4j
@Api(tags = "文档采集-ES使用")
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestUseEeController {

    private final DocumentMapper documentMapper;

    @ApiOperation(value = "创建索引")
    @GetMapping("/createIndex")
    public Boolean createIndex() {
        // 初始化-> 创建索引
        return documentMapper.createIndex();
    }


    @ApiOperation(value = "文档入库")
    @PostMapping("/insert")
    public Integer insert(@RequestBody DocumentDTO body) {
        // 初始化-> 新增数据
        Document document = new Document();
        document.setId(IdUtil.fastSimpleUUID());
        document.setTitle(body.getTitle());
        document.setContent(body.getContent());
        return documentMapper.insert(document);
    }


    @ApiOperation(value = "文档查询")
    @GetMapping("/search")
    public List<Document> search(String title, String content) {

        // 查询出所有条件文档列表
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(title), Document::getTitle, title)
                .or().like(StringUtils.isNotBlank(content), Document::getContent, content);

        String source = documentMapper.getSource(wrapper);
        log.info("=================" + source);

        return documentMapper.selectList(wrapper);
    }

}
